package cn.wolfcode.trip.base.service.impl;


import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyContent;
import cn.wolfcode.trip.base.domain.Praise;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.DailyContentMapper;
import cn.wolfcode.trip.base.mapper.DailyMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DailyServiceImpl implements IDailyService{
    @Autowired
    private DailyMapper dailyMapper;

    @Autowired
    private DailyContentMapper contentMapper;

    @Override
    public void saveOrUpdate(Daily daily) {
        DailyContent content = daily.getDailyContent();

        if(daily.getId() == null){

            //保存操作.系统指定日报创建时间
            daily.setCreatTime(new Date());

            dailyMapper.insert(daily);
            //日报保存后同步保存日报内容
            content.setId(daily.getId());
            contentMapper.insert(content);


        } else {

            dailyMapper.updateByPrimaryKey(daily);

            //日报更新后后同步更新日报内容
            content.setId(daily.getId());
            contentMapper.updateByPrimaryKey(content);

        }
    }


    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Daily> list = dailyMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public Daily queryDailyById(Long id) {
        Daily daily = dailyMapper.selectByPrimaryKey(id);
        return daily;
    }

    @Override
    public Boolean queryIsLike(Long dailyId, Integer state) {
        User user = UserContext.getUserAttribute();
        String s=dailyMapper.queryIsLike(dailyId,user.getId(),state);
        if(s==null){
            return false;
        }
        return true;
    }

    @Override
    public void saveDailyIsLike(Praise obj) {
        dailyMapper.saveDailyIsLike(obj);
    }

    @Override
    public void deleteDailyIsLike(Praise obj) {
        dailyMapper.deleteDailyIsLike(obj);
    }

    @Override
    public Daily selectById(Long id) {
        return dailyMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateNum(Long id) {
        dailyMapper.updateNum(id);
    }


}
